package util.qualitycontainers.qualities;

import util.qualitycontainers.MorphException;


public abstract class MorphQuality implements IMorphQuality, Cloneable
{

    public MorphQuality ()
    {}


    public MorphQuality (MorphQuality aq)
    {
        if (this.isSameQualityClass(aq)) this.setTo(aq);
        throw new MorphException(MorphException.DIFFERENT_QUALITIES);
    }


    /*
     * @see images.IQuality#isSameQualityClass(images.IQuality)
     */
    @Override
    public boolean isSameQualityClass (IMorphQuality other)
    {

        return isSameQualityClass(this.getClass());

    }


    /*
     * @see images.IQuality#isSameQualityClass(java.lang.Class)
     */
    @Override
    public boolean isSameQualityClass (Class<? extends IMorphQuality> cls)
    {

        return this.getClass().equals(cls);
    }


    @Override
    public int compareTo (IMorphQuality o)
    {
        ;
        if (!this.isSameQualityClass(o)) throw new MorphException(MorphException.DIFFERENT_QUALITIES);
        return this.compareTo((MorphQuality) o);
    }


    @Override
    public IMorphQuality clone ()
    {
        try
        {
            return this.getClass()
                       .getConstructor(this.getClass())
                       .newInstance(this);
        }
        catch (Exception e)
        {
            throw new MorphException(MorphException.ERROR_IN_CLONING);
        }
    }


    @Override
    public final boolean equals (IMorphQuality other)
    {
        if (other == this)
        {
            return true;
        }
        if (other instanceof IMorphQuality)
        {
            return equals((MorphQuality) other);
        }
        return false;
    }


    abstract protected int compareTo (MorphQuality o);


    public abstract boolean equals (MorphQuality aq);
}
